/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.unittests.tests;

import j2meunit.framework.Test;
import j2meunit.framework.TestCase;
import j2meunit.framework.TestMethod;
import j2meunit.framework.TestSuite;

import java.util.Vector;

import javax.microedition.lcdui.Alert;
import javax.microedition.location.AddressInfo;
import javax.microedition.location.Landmark;
import javax.microedition.location.QualifiedCoordinates;


import com.aroundj2me.model.Constants;
import com.aroundj2me.model.Place;
import com.aroundj2me.xml.XMLDecoder;

public class XMLDecoderTest extends TestCase {
	private XMLDecoder decoder;
	
	public XMLDecoderTest() {
	}
	
	public XMLDecoderTest(String string, TestMethod method) {
		super(string, method);
	}
	
	protected void setUp() throws Exception {
		decoder = new XMLDecoder();
	}

	protected void tearDown() throws Exception {
		decoder = null;
	}

	/**
	 * Checks specifically the retrieving of place-objects
	 * out of the xml-response string returned from the server
	 *
	 */
	public void testGetPlaces(){
		Place p = new Place();
		p.setContinent("Europe");
		QualifiedCoordinates coordinates = new QualifiedCoordinates(46.498215,11.3510341 , 0f, 0f, 0f);	
	
		AddressInfo addressInfo = new AddressInfo();
		addressInfo.setField(AddressInfo.STREET, "Via Sernesi 1");
		addressInfo.setField(AddressInfo.POSTAL_CODE, "39100");
		addressInfo.setField(AddressInfo.CITY, "Bolzano");
		addressInfo.setField(AddressInfo.COUNTRY, "Italy");
		addressInfo.setField(AddressInfo.PHONE_NUMBER, "0471 913829");
		addressInfo.setField(AddressInfo.URL, "http://www.unibz.it");
		
		String[] categories = new String[]{"Bar"};
		p.setCategories(categories);
		
		Landmark landmark = new Landmark("Unibz", p.getCategoriesAsString(), coordinates, addressInfo);
		p.setLandmark(landmark);
		
		
		StringBuffer dummyResponse = new StringBuffer();
		dummyResponse.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
		dummyResponse.append("<root>");
		dummyResponse.append("<type>" + Constants.DATA_PLACE + "</type>");
		dummyResponse.append("<size>1</size>");
		dummyResponse.append("<responseBody>");
		dummyResponse.append("<place>");
		dummyResponse.append("<name>" + p.getLandmark().getName() + "</name>");
		dummyResponse.append("<address>" + p.getLandmark().getAddressInfo().getField(AddressInfo.STREET) + "</address>");
		dummyResponse.append("<postalCode>" + p.getLandmark().getAddressInfo().getField(AddressInfo.POSTAL_CODE) + "</postalCode>");
		dummyResponse.append("<city>" + p.getLandmark().getAddressInfo().getField(AddressInfo.CITY) + "</city>");
		dummyResponse.append("<country>" + p.getLandmark().getAddressInfo().getField(AddressInfo.COUNTRY) + "</country>");
		dummyResponse.append("<continent>" + p.getContinent() + "</continent>");
		dummyResponse.append("<phone>" + p.getLandmark().getAddressInfo().getField(AddressInfo.PHONE_NUMBER) + "</phone>");
		dummyResponse.append("<url>" + p.getLandmark().getAddressInfo().getField(AddressInfo.URL) + "</url>");
		dummyResponse.append("<categories>");
		dummyResponse.append("<category>" + p.getCategories()[0].toString() + "</category>");
		dummyResponse.append("</categories>");
		dummyResponse.append("<location>");
		dummyResponse.append("<lat>46.499855</lat>");
		dummyResponse.append("<lon>11.352568</lon>");
		dummyResponse.append("</location>");
		dummyResponse.append("</place>");
		dummyResponse.append("</responseBody>");
		dummyResponse.append("</root>");
		
		decoder.setXmlResponseString(dummyResponse.toString());
		try {
			decoder.parseXML();
		} catch (Exception e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
		
		Vector places = decoder.getPlaces();
		
		assertNotNull(places);
		assertEquals("Number of returned places", 1, places.size());		
		Place pRetrieved = (Place) places.elementAt(0);
		
		assertEquals("name of the landmark", p.getLandmark().getName(), pRetrieved.getLandmark().getName());
		assertEquals("street of the landmark", p.getLandmark().getAddressInfo().getField(AddressInfo.STREET), pRetrieved.getLandmark().getAddressInfo().getField(AddressInfo.STREET));
		assertEquals("postal code of the landmark", p.getLandmark().getAddressInfo().getField(AddressInfo.POSTAL_CODE),pRetrieved.getLandmark().getAddressInfo().getField(AddressInfo.POSTAL_CODE));
		assertEquals("city of the landmark", p.getLandmark().getAddressInfo().getField(AddressInfo.CITY),pRetrieved.getLandmark().getAddressInfo().getField(AddressInfo.CITY));
		assertEquals("country of the landmark", p.getLandmark().getAddressInfo().getField(AddressInfo.COUNTRY), pRetrieved.getLandmark().getAddressInfo().getField(AddressInfo.COUNTRY));
		assertEquals("continent of the landmark", p.getContinent(), pRetrieved.getContinent());
		assertEquals("phone of the landmark", p.getLandmark().getAddressInfo().getField(AddressInfo.PHONE_NUMBER), pRetrieved.getLandmark().getAddressInfo().getField(AddressInfo.PHONE_NUMBER));
		assertEquals("url of the landmark", p.getLandmark().getAddressInfo().getField(AddressInfo.URL), pRetrieved.getLandmark().getAddressInfo().getField(AddressInfo.URL));
		assertEquals("categories", p.getCategoriesAsString(), pRetrieved.getCategoriesAsString());
		assertNotNull("latitude", String.valueOf(pRetrieved.getLandmark().getQualifiedCoordinates().getLatitude()));
		assertNotNull("latitude", String.valueOf(pRetrieved.getLandmark().getQualifiedCoordinates().getLongitude()));
		
	}
	
	/**
	 * Checks if an Alert object is parsed out successfully
	 * out of the server response
	 *
	 */
	public void testGetAlert(){
		String alertMessage = "hello, message test";
		
		StringBuffer dummyResponse = new StringBuffer();
		dummyResponse.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
		dummyResponse.append("<root>");
		dummyResponse.append("<type>" + Constants.ALERT + "</type>");
		dummyResponse.append("<size/>");
		dummyResponse.append("<responseBody>");
		dummyResponse.append("<level>" + Constants.ALERT_ERROR + "</level>");
		dummyResponse.append("<message>" + alertMessage + "</message>");
		dummyResponse.append("</responseBody>");
		dummyResponse.append("</root>");
		
		decoder.setXmlResponseString(dummyResponse.toString());
		try {
			decoder.parseXML();
		} catch (Exception e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
		
		Alert parsedAlert = decoder.getAlert();
		
		assertNotNull(parsedAlert);
		assertEquals("Type should be " + Constants.ALERT, Constants.ALERT, decoder.getType());
		assertEquals("Alert-level should be " + Constants.ALERT_ERROR, Constants.ALERT_ERROR, parsedAlert.getTitle());
		assertEquals("Message should be equal", alertMessage, parsedAlert.getString());
		
	}


	public Test suite() {
		TestSuite suite = new TestSuite();
		suite.addTest(new XMLDecoderTest("XMLDecoderTest:testGetPlaces", new TestMethod() {
			public void run(TestCase tc) {
				((XMLDecoderTest) tc).testGetPlaces();
			}
		}));
		suite.addTest(new XMLDecoderTest("XMLDecoderTest:testGetAlert", new TestMethod() {
			public void run(TestCase tc) {
				((XMLDecoderTest) tc).testGetAlert();
			}
		}));
		return suite;
	}
}
